Added zip code as an option for previous installations

Joseph Scavone 10 年 前
コミット
8bd5607275
共有1 個のファイルを変更した8 個の追加4 個の削除を含む
  1. 8 4
      app/models/agents/weather_agent.rb

+ 8 - 4
app/models/agents/weather_agent.rb

@@ -73,22 +73,26 @@ module Agents
73 73
       (options["which_day"].presence || 1).to_i
74 74
     end
75 75
 
76
+    def location
77
+      options["location"].presence || options["zipcode"]
78
+    end
79
+
76 80
     def validate_options
77 81
       errors.add(:base, "service is required") unless service.present?
78 82
       errors.add(:base, "service must be set to 'forecastio' or 'wunderground'") unless ["forecastio", "wunderground"].include?(service)
79
-      errors.add(:base, "location is required") unless options['location'].present?
83
+      errors.add(:base, "location is required") unless location.present?
80 84
       errors.add(:base, "api_key is required") unless key_setup?
81 85
       errors.add(:base, "which_day selection is required") unless which_day.present?
82 86
     end
83 87
 
84 88
     def wunderground
85
-      Wunderground.new(options['api_key']).forecast_for(options['location'])['forecast']['simpleforecast']['forecastday'] if key_setup?
89
+      Wunderground.new(options['api_key']).forecast_for(location)['forecast']['simpleforecast']['forecastday'] if key_setup?
86 90
     end
87 91
 
88 92
     def forecastio
89 93
       if key_setup?
90 94
         ForecastIO.api_key = options['api_key']
91
-        lat, lng = options['location'].split(',')
95
+        lat, lng = location.split(',')
92 96
         ForecastIO.forecast(lat,lng)['daily']['data']
93 97
       end
94 98
     end
@@ -166,7 +170,7 @@ module Agents
166 170
 
167 171
     def check
168 172
       if key_setup?
169
-        create_event :payload => model(service, which_day).merge('location' => options['location'])
173
+        create_event :payload => model(service, which_day).merge('location' => location)
170 174
       end
171 175
     end
172 176